import json as js

from flask import Flask, request
from flask import jsonify
from flask import Response
from flask import session

app = Flask(__name__)
app.secret_key = "ashdhjjtrtaretrshdgbgvfcx"

@app.get("/")
def index():
    return "<h1>处理requests请求</h1>"


@app.get("/response_type")
def response_type():
    # return "哈哈哈哈哈"
    return "哈哈哈哈哈".encode(encoding="GBK")
    return Response("哈哈哈哈哈".encode(encoding="GBK"), content_type="text/html；charset=gbk")
    # 此时Content-Type还是text/html
    # return json.dumps({"info": "哈哈哈哈哈"})
    # 此时Content-Type还是application / json
    # return jsonify({"info": "哈哈哈哈哈"})


@app.get("/params")
def get_params():
    page = request.args.get("page", 1)
    size = request.args.get("size", 10)
    next = request.args.get("next", "/")
    sort = request.args.get("sort")
    result = {
        "code": 0,
        "args": request.args,
        "headers": {
            "User-Agent": request.headers["User-Agent"]
        }
    }
    return jsonify(result)


@app.post("/data")
def data():
    result = {
        "code": 0,
        "form": request.form,
        "headers": {
            "User-Agent": request.headers["User-Agent"]
        }
    }
    return jsonify(result)


@app.post("/json")
def json():
    name = request.json.get("name")
    password = request.json.get("password")
    if not name or not password:
        return jsonify({"msg": "参数错误"}), 400

    result = {
        "code": 0,
        "json": request.json,
        "headers": {
            "User-Agent": request.headers["User-Agent"]
        }
    }
    return jsonify(result)


@app.get("/headers")
def headers():
    # UA限制
    ua = request.headers["User-Agent"]
    if ua.startswith("python"):
        result = {
            "code": 0,
            "msg": "未知来源错误"
        }
    else:
        # Referer 反爬
        referer = request.headers.get("Referer", None)
        if not referer or not referer.startswith("http://127.0.0.1:5000/"):
            result = {
                "code": 0,
                "msg": "未知来源错误"
            }
        else:
            result = {
                "code": 0,
                "headers": {
                    "User-Agent": request.headers["User-Agent"],
                    "Referer": request.headers["Referer"],
                }
            }
    return jsonify(result)


@app.post("/login")
def login():
    username = request.json.get("username", None)
    password = request.json.get("password", None)
    if not password or not username:
        result = {
            "code": 0,
            "msg": "用户名、密码必填"
        }
        return jsonify(result), 400
    else:
        if username == "admin" and password == "123456":
            result = {
                "code": 0,
                "msg": "登录成功"
            }
            session["user"] = js.dumps({
                "id": 101,
                "username": username,
                "joined_date": "2001-01-01"
            })
            return jsonify(result), 200
        else:
            result = {
                "code": 0,
                "msg": "用户名或密码错误"
            }
        return jsonify(result), 400


@app.get("/center")
def center():
    user = session.get("user")
    if user:
        result = {
            "code": 0,
            "msg": "获取用户信息成功",
            "data": js.loads(user)
        }
        return jsonify(result)
    else:
        result = {
            "code": 0,
            "msg": "请登录"
        }
        return jsonify(result), 401


app.run(debug=True)
